package com.dhecp.project.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysUserDefinedForField;
import com.dhecp.project.system.mapper.SysUserDefinedForFieldMapper;
import com.dhecp.project.system.service.SysUserDefinedForFieldService;

/**
 * 用户自定义列设置
 * @author DH-Xxj
 * @version 2019年11月11日
 */

@Service
public class SysUserDefinedForFieldServiceImpl implements SysUserDefinedForFieldService {

	@Autowired
	private SysUserDefinedForFieldMapper userDefinedForFieldMapper;

	/**
	 * 获取用户自定义列设置的字段及各属性
	 * @param moduleTable 必要参数：功能唯一标识
	 * @return List-SysUserDefinedForField
	 * @author DH-Xxj
	 */
	@Override
	public List<SysUserDefinedForField> queryUserDefinedForField(Map<String, Object> params) {
    	params.put("loginUser", ShiroUtils.getUserId()); //当前登录用户
		return userDefinedForFieldMapper.findUserDefinedForField(params);
	}

	/**
	 * 更新用户自定义列设置
	 * @param moduleTable 必要参数：功能唯一标识
	 * @param loginUser 必要参数：当前登录用户
	 * @param infoItems 用户自定义设置实体类对应的json格式字符串
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult updateUserDefinedForField(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysUserDefinedForField> fieldList = JSON.parseArray(params.get("infoItems").toString(), SysUserDefinedForField.class);
		if (fieldList == null || fieldList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			return AjaxResult.error("缺少必要参数：moduleTable【功能唯一标识】。");
		}
    	params.put("loginUser", ShiroUtils.getUserId()); //当前登录用户
		userDefinedForFieldMapper.deleteFields(params);
		for (SysUserDefinedForField userDefinedForField : fieldList) {
			userDefinedForField.setUserId(ShiroUtils.getUserId());
			userDefinedForField.setModuleTable(params.get("moduleTable").toString());
			userDefinedForFieldMapper.insert(userDefinedForField);
		}
		return AjaxResult.success("操作成功！");
	}

	/**
	 * 恢复系统设置（删除用户自定义设置列）
	 * @param moduleTable 必要参数：功能唯一标识
	 * @param loginUser 必要参数：当前登录用户
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult deleteUserDefinedForField(Map<String, Object> params){
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			return AjaxResult.error("缺少必要参数：moduleTable【功能唯一标识】。");
		}
    	params.put("loginUser", ShiroUtils.getUserId()); //当前登录用户
		userDefinedForFieldMapper.deleteFields(params);
		return AjaxResult.success("操作成功！");	
	}
	
}
